package com.example.boot6rabbitmq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.boot6rabbitmq.config.MessagePublishConfirmSender;
import com.example.boot6rabbitmq.config.RabbitmqSender;
import com.example.boot6rabbitmq.entity.Message;
import com.example.boot6rabbitmq.service.MessageService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    RabbitmqSender rabbitmqSender;

    @Override
    public String send(int num,String msg1,String msg2) {
        System.out.println("---rabbitmq send start---");
        List<Message> messages =new ArrayList<>();

        for (int i = 1; i <= num ; i++) {
            Message m1 = new Message();
            m1.setMessage_id(i);
            m1.setAmount(10+i);
            m1.setState(i+msg1);
            m1.setUser_id(i+msg2);
            messages.add(m1);
        }

        System.out.println("=======messages:" + JSONObject.toJSONString(messages));
        for (Message message : messages) {
            System.out.println("=======调用接口向rabbitmq发送消息:" + JSONObject.toJSONString(message));
            rabbitmqSender.sendMessage("exchange.message", "chenfl.message.routeKey", message);
        }

        return " send ok! ";
    }

    @Override
    public String send1(int num, String msg1, String msg2) {
        System.out.println("---rabbitmq send1 start---");
        List<Message> messages =new ArrayList<>();

        for (int i = 1; i <= num ; i++) {
            Message m1 = new Message();
            m1.setMessage_id(i);
            m1.setAmount(20+i);
            m1.setState(i+msg1);
            m1.setUser_id(i+msg2);
            messages.add(m1);
        }

        System.out.println("====1===messages:" + JSONObject.toJSONString(messages));
        for (Message message : messages) {
            System.out.println("===1====调用接口向rabbitmq发送消息:" + JSONObject.toJSONString(message));
            rabbitmqSender.sendMessageConfirm("exchange.message1", "chenfl.message.routeKey-gy", message);
        }
        return " send1 ok!";
    }


}
